package com.hx0311.pas.dept.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hx0311.core.dao.BaseDao;
import com.hx0311.core.dao.support.ResultSetUtil;
import com.hx0311.pas.dept.model.Jgdy;

public class JgdyDao extends BaseDao {

	private Connection conn;
	private PreparedStatement stat;

	public void add(Jgdy jgdy) {
		if (jgdy != null) {
			String sql = "insert into jgdy (jgm, jgmc, jglx) values (?, ?, ?)";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setString(1, jgdy.getJgm());
				stat.setString(2, jgdy.getJgmc());
				stat.setString(3, jgdy.getJglx());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				close(stat, conn);
			}
		}
	}

	public void update(Jgdy jgdy) {
		if (jgdy != null) {
			String sql = "update jgdy set jgmc = ?, jglx = ? where jgm = ?";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setString(1, jgdy.getJgmc());
				stat.setString(2, jgdy.getJglx());
				stat.setString(3, jgdy.getJgm());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				close(stat, conn);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List getAll() {
		String sql = "select jgm, jgmc, jglx from jgdy";
		ResultSet rs = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			return ResultSetUtil.toList(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs, stat, conn);
		}
		return java.util.Collections.EMPTY_LIST;
	}

	@Override
	public String getIdName() {
		return "jgm";
	}

	@Override
	public String getTableName() {
		return "jgdy";
	}
}
